var currentPage = 1;

$(function() {
	$("#startDate").val(getCurrentDate());
	$("#endDate").val(getCurrentDate());
	doSearch();
	selectPage();
});

function getCurrentDate() {
	var now=new Date();
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var day = now.getDate();
	month = month <10 ? "0"+ month:month;
	day = day < 10 ? "0" + day:day;
	return year + "-" + month + "-" + day;
}

function checkTime(startDate, endDate) {
	if (startDate == "" && endDate != "") {
		showMsg("请选择起始时间");
		return false;
	}
	
	if (startDate != "" && endDate == "") {
		showMsg("请选择结束时间");
		return false;
	}
	var data1 = Date.parse(startDate.replace(/-/g, "/"));
   	var data2 = Date.parse(endDate.replace(/-/g,"/"));
   	var time = 30*24*60*60*1000;
   	if (data2 - data1 > time) {
   		showMsg("查询范围不得超过三十天");
			return false;
		}
   	if (data1 > data2) {
   		showMsg("结束时间应比开始时间晚");
			return false;
		}
   	return true;
}   

function resetValues() {
	$("#startDate").val("");
	$("#endDate").val("");
	$("#disburseNo").val("");
	$("#memberName").val("");
	$("#phone").val("");
	$("#status").val("");
}

function queryData() {
	currentPage = 1; //搜索的时候不限制页码范围
	doSearch();
}
    	
function doSearch() {
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
   	if (checkTime(startDate, endDate)) {
   		var disburseNo = $("#disburseNo").val();
		var buyerMemberName = $("#buyerMemberName").val();
		var phone = $("#phone").val();
		var status = $("#status").val();
		var postData = {pageNum: currentPage, startDate: startDate, endDate: endDate, disburseNo: disburseNo, 
				        buyerMemberName: buyerMemberName, status: status, phone: phone};
		$.ajax({
	      url: appServer + "/back/tran/doSearch.htm",
	      type: 'post',
	      dataType: 'html',
	      data: postData,
	      success: function(data) {
	      	if (data == "" || data == null) {
	      		noDataDisplay();
	     	} else {
	     		showDataList(data);
	     	}
	      	
	      },
	      error: function(){
	      }
	  });
   }
}

function updateDisburse(disburseNo, status) {
	$("#ustatus").val(status);
	var postData = {disburseNo: disburseNo};
	$.ajax({
        type: "POST",
        url: appServer + "/back/tran/modifyDisburse.htm",
        data: postData,
        dataType: "json",
        success: function(data) {
        	$("#uDisburseNo").val(data.data.disburseNo);
        	$("#logisticsCompany").val(data.data.logisticsCompany);
        	$("#logisticsNo").val(data.data.logisticsNo);

			$('#myModal').modal({backdrop: 'static', keyboard: false});
        }
        
  	});
}



function detailDisburse(disburseNo) {
	var postData = {disburseNo: disburseNo};
	$.ajax({
        type: "POST",
        url: appServer + "/back/tran/modifyDisburse.htm",
        data: postData,
        dataType: "json",
        success: function(data) {
        	$("#dDisburseNo").text(data.data.disburseNo);
        	if(data.data.receiveName != null) {
        		$("#dReceiveName").text(data.data.receiveName);
        	} else {
        		$("#dReceiveName").text("");
        	}
        	if(data.data.phone != null) {
        		$("#dPhone").text(data.data.phone);
        	} else {
        		$("#dPhone").text("");
        	}
        	if(data.data.addressDetail != null) {
        		$("#daddressDetail").text(data.data.addressDetail);
        	} else {
        		$("#daddressDetail").text("");
        	}
        	if(data.data.zipCode != null) {
        		$("#dzipCode").text(data.data.zipCode);
        	} else {
        		$("#dzipCode").text("");
        	}
        	if(data.data.logisticsCompany != null) {
        		$("#dlogisticsCompany").text(data.data.logisticsCompany);
        	} else {
        		$("#dlogisticsCompany").text("");
        	}
        	if(data.data.logisticsNo != null) {
        		$("#dlogisticsNo").text(data.data.logisticsNo);
        	} else {
        		$("#dlogisticsNo").text("");
        	}
        	
        	if(data.data.snapshot.color != null) {
        		$("#dcolor").text(data.data.snapshot.color);
        	} else {
        		$("#dcolor").text("");
        	}
        	
        	if(data.data.snapshot.material != null) {
        		$("#dmaterial").text(data.data.snapshot.material);
        	} else {
        		$("#dmaterial").text("");
        	}
        	
        	if(data.data.snapshot.itemCode != null) {
        		$("#ditemCode").text(data.data.snapshot.itemCode);
        	} else {
        		$("#ditemCode").text("");
        	}
        	
        	if(data.data.snapshot.referencePriceShow != null) {
        		$("#dreferencePriceShow").text(data.data.snapshot.referencePriceShow);
        	} else {
        		$("#dreferencePriceShow").text("");
        	}
        	
        	if(data.data.snapshot.curno != null) {
        		$("#dcurno").text(data.data.snapshot.curno);
        	} else {
        		$("#dcurno").text("");
        	}
        	
			$('#detailModal').modal({backdrop: 'static', keyboard: false});
        }
        
  	});
}


function update() {
	var saveUrl = "";
	var uDisburseNo = $("#uDisburseNo").val();
	if (uDisburseNo == null || uDisburseNo == "") {
		$("#uDisburseNo").attr('title', "订单号不能为空").tooltip('show');
		return;
	}
	$("#logisticsCompany").removeAttr('title');
	var logisticsCompany = $("#logisticsCompany").val();
	if (logisticsCompany == null || logisticsCompany == "") {
		$("#logisticsCompany").attr('title', "物流公司不能为空").tooltip('show');
		return;
	}
	
	$("#logisticsNo").removeAttr('title');
	var logisticsNo = $("#logisticsNo").val();
	if (logisticsNo == null || logisticsNo == "") {
		$("#logisticsNo").attr('title', "物流订单号").tooltip('show');
		return;
	}
	
	var status = $("#ustatus").val();
	saveUrl = appServer + "/back/tran/updateDisburse.htm";
	
	var postData = {disburseNo: uDisburseNo, logisticsCompany: logisticsCompany, status: status, logisticsNo: logisticsNo};
	
	$.ajax({
      url: saveUrl,
      type: 'post',
      dataType: 'json',
      data: postData,
      success: function(data) {
    	  if (data.success == true) {
    		  $("#myModal").modal('hide');
    		  doSearch();
    		  
    	  } else {
    		  swal("操作失败！", data.msg, "error");
    	  }
      },
      error: function(){
      }
  });
	
}


function readyGoods(disburseNo, status) {
	
	var postData = {disburseNo: disburseNo, status: status};
	
	$.ajax({
      url: appServer + "/back/tran/updateDisburse.htm",
      type: 'post',
      dataType: 'json',
      data: postData,
      success: function(data) {
    	  if (data.success == true) {
    		  doSearch();
    	  } else {
    		  swal("操作失败！", data.msg, "error");
    	  }
      },
      error: function(){
      }
  });
	
}


function cancelReadyGoods(disburseNo, status) {
	
	var postData = {disburseNo: disburseNo, status: status};
	
	$.ajax({
      url: appServer + "/back/tran/updateDisburse.htm",
      type: 'post',
      dataType: 'json',
      data: postData,
      success: function(data) {
    	  if (data.success == true) {
    		  doSearch();
    	  } else {
    		  swal("操作失败！", data.msg, "error");
    	  }
      },
      error: function(){
      }
  });
	
}

function cancelManager(disburseNo, status) {
	
	var postData = {disburseNo: disburseNo, status: status};
	
	$.ajax({
      url: appServer + "/back/tran/updateDisburse.htm",
      type: 'post',
      dataType: 'json',
      data: postData,
      success: function(data) {
    	  if (data.success == true) {
    		  doSearch();
    	  } else {
    		  swal("操作失败！", data.msg, "error");
    	  }
      },
      error: function(){
      }
  });
	
}